package com.greenantmall.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;
import com.greenantmall.R;
import com.greenantmall.service.RetrieveDataAsyn;
import com.greenantmall.service.RetrieveDataListener;
import com.greenantmall.utils.UtilTools;
import com.lsarah.utils.SystemBarTintManager;

public class EvaluationFeedbackActivity extends BaseActivity{
	private Button mBtnBack;
	private TextView mTvCommit;
	private EditText mEtContent;
	private EditText mEtEmail;
	
	private boolean isCommitting = false;
	
	private OnClickListener mOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if(v.equals(mBtnBack)) {
				EvaluationFeedbackActivity.this.finish();
			}
			else if(v.equals(mTvCommit)) {
				if(isCommitting) {
					UtilTools.showMyToast(EvaluationFeedbackActivity.this, 
							getResources().getString(R.string.evaluation_feedback_commiting));
					return;
				}
				
				String content = mEtContent.getText().toString();
				if(TextUtils.isEmpty(content)) {
					UtilTools.showMyToast(EvaluationFeedbackActivity.this, 
							getResources().getString(R.string.evaluation_feedback_no_content));
					return;
				}
				
				String email = mEtEmail.getText().toString();
				email = (email==null ? "" : email);
				if(!TextUtils.isEmpty(email)) {
					if(!UtilTools.isEmail(email)) {
						UtilTools.showMyToast(EvaluationFeedbackActivity.this, 
								getResources().getString(R.string.evaluation_feedback_email_format_error));
						return;
					}
				}
				
				isCommitting = true;
				String evaluationFeedback = content + "(Email:" + email + ")";
				Log.d("CEXX", "CEXX ---> EvaluationFeedbackActivity ---> evaluationFeedback : " + evaluationFeedback);
				RetrieveDataAsyn.getInstance().commentFeedback(
						AVUser.getCurrentUser().getObjectId(), 
						evaluationFeedback, 
						-1, new RetrieveDataListener() {
							@Override
							public void dataRetrieved(int requestId, Object data) {
								isCommitting = false;
								if(data != null) {
									UtilTools.showMyToast(EvaluationFeedbackActivity.this, 
											getResources().getString(R.string.evaluation_feedback_committed));
									EvaluationFeedbackActivity.this.finish();
								}
								else {
									UtilTools.showMyToast(EvaluationFeedbackActivity.this, 
											getResources().getString(R.string.evaluation_feedback_committed_failed));
								}
							}
						});
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SystemBarTintManager.setContentView(this, R.layout.evaluation_feedback, getResources().getColor(R.color.green));
		mBtnBack = (Button)findViewById(R.id.evaluation_feedback_top_arrow_left);
		mTvCommit = (TextView)findViewById(R.id.evaluation_feedback_top_tv_edit);
		mEtContent = (EditText)findViewById(R.id.evaluation_feedback_et);
		mEtEmail = (EditText)findViewById(R.id.evaluation_feedback_et_email);
		mBtnBack.setOnClickListener(mOnClickListener);
		mTvCommit.setOnClickListener(mOnClickListener);
	}
}
